﻿using FluentValidation;
using SDF.Core.Data;
using SDF.Data;
using SDF.Plugin.Misc.DynamicEntity.Domain;
using SDF.Plugin.Misc.DynamicEntity.Models;
using SDF.Services.Localization;
using SDF.Web.Framework.Validators;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace SDF.Plugin.Misc.DynamicEntity.Validators.DynamicEntity
{
    public class DynamicEntityHeaderValidator : BaseSDFValidator<DynamicEntityHeaderModel>
    {
        public DynamicEntityHeaderValidator(ILocalizationService localizationService, IDbContext dbContext, IRepository<DynamicEntityHeader> entityRepository)
        {
            RuleFor(x => x.CodeName)
                .NotEmpty()
                .WithMessage(localizationService.GetResource("Plugins.Misc.DynamicEntity.CodeName.Required"));

            RuleFor(x => x.Name)
                .NotEmpty()
                .WithMessage(localizationService.GetResource("Plugins.Misc.DynamicEntity.Name.Required"));

            RuleFor(x => x.Name).Must(x =>
            {
                //DynamicEntity的CodeName不能重复
                return entityRepository.Table.Any(p => p.CodeName.Equals(x)) == false;
            }).WithMessage(localizationService.GetResource("Plugins.Misc.DynamicEntity.CodeName.Unique"));

            SetStringPropertiesMaxLength<DynamicEntityItemModel>(dbContext);
        }
    }
}